﻿using System.Runtime.Serialization;
using EPiServer.Data;
using EPiServer.Data.Dynamic;

namespace EPiBoost.Core.Infrastructure.CustomRedirects
{
		[EPiServer.Data.Dynamic.EPiServerDataStoreAttribute(AutomaticallyRemapStore = true, AutomaticallyCreateStore = true)]
		[DataContract]
    public class CustomRedirect : IDynamicData
    {
        private string _oldUrl;
        private string _newUrl; 
        private bool _wildCardSkipAppend = false;
				private bool _overrideExisting = false;
				private bool _maintainQuerystring = false;
				private int _requestCount;
        private bool _matchStart;

				[DataMember]
        public bool MatchStart
        {
            get { return _matchStart; }
            set { _matchStart = value; }
        }

        /// <summary>
        /// The number of times the oldurl url has been requested and unhandled
				/// </summary>
				[DataMember]
        public int RequestCount
        {
            get { return _requestCount; }
            set { _requestCount = value; }
        } 

        /// <summary>
        /// Gets or sets a value indicating whether to skip appending the
        /// old url fragment to the new one. Default value is false.
        /// </summary>
        /// <remarks>
        /// If you want to redirect many addresses below a specifc one to
        /// one new url, set this to true. If we get a wild card match on
        /// this url, the new url will be used in its raw format, and the
        /// old url will not be appended to the new one.
        /// </remarks>
        /// <value><c>true</c> to skip appending old url if wild card match; otherwise, <c>false</c>.</value>
        [DataMember]
        public bool WildCardSkipAppend
        {
            get
            {
                return _wildCardSkipAppend;
            }
            set
            {
                _wildCardSkipAppend = value;
            }
        }

				[DataMember]
				public bool OverrideExisting
				{
					get
					{
						return _overrideExisting;
					}
					set
					{
						_overrideExisting = value;
					}
				}

				[DataMember]
				public bool MaintainQuerystring
				{
					get
					{
						return _maintainQuerystring;
					}
					set
					{
						_maintainQuerystring = value;
					}
				}

				[DataMember]
        public string OldUrl
        {
            get
            {
                return _oldUrl.ToLower();
            }
            set
            {
                _oldUrl = value;
            }
        }

				[DataMember]
        public string NewUrl
        {
            get
            {
                return _newUrl.ToLower();
            }
            set
            {
                _newUrl = value;
            }
        }

        public bool HasRedirectUrl { get { return !string.IsNullOrEmpty(_newUrl) && _newUrl.ToLower() != _oldUrl.ToLower(); } }

        /// <summary>
        /// Tells if the new url is a virtual url, not containing
        /// the base root url to redirect to. All urls starting with
        /// "/" is determined to be virtuals.
        /// </summary>
        public bool IsVirtual
        {
            get
            {
                if (_newUrl.StartsWith("/"))
                    return true;
                return false;
            }
        }

        /// <summary>
        /// The hash code for the CustomRedirect class is the
        /// old url string, which is the one we'll be doing lookups
        /// based on.
        /// </summary>
        /// <returns>The Hash code of the old Url</returns>
        public override int GetHashCode()
        {

            //TODO: should not have to check for null
            return _oldUrl != null ? _oldUrl.GetHashCode() : 0;
        }

        public Identity Id { get; set; }


        #region constructors...
        public CustomRedirect()
        {
        }

        public CustomRedirect(string oldUrl, string newUrl, bool skipWildCardAppend, int requestCount, bool matchStart)
            : this(oldUrl, newUrl)
        {
            _wildCardSkipAppend = skipWildCardAppend;
            _requestCount = requestCount;
            _matchStart = matchStart;
        }

        public CustomRedirect(string oldUrl, string newUrl)
        {
            _oldUrl = oldUrl;
            _newUrl = newUrl;
        }
        public CustomRedirect(CustomRedirect redirect)
        {
            _oldUrl = redirect._oldUrl;
            _newUrl = redirect._newUrl;
            _wildCardSkipAppend = redirect._wildCardSkipAppend;
            _requestCount = redirect._requestCount;
            _matchStart = redirect._matchStart;
        }
        #endregion



    }
}